package com.whfc.wxmp.api.fse.service.impl;

import com.whfc.common.enums.BindFlag;
import com.whfc.common.result.ListData;
import com.whfc.common.util.DateUtil;
import com.whfc.fse.dto.FseCraneDTO;
import com.whfc.fse.dto.FseCraneDataDTO;
import com.whfc.fse.dto.FseCraneDataLogDTO;
import com.whfc.fse.dto.FseCraneFvsDeviceDTO;
import com.whfc.fse.service.FseCraneDataService;
import com.whfc.fse.service.FseCraneService;
import com.whfc.fvs.dto.FvsDeviceDTO;
import com.whfc.fvs.service.FvsDeviceService;
import com.whfc.wxmp.api.fse.service.WxFseCraneService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author qzexing
 * @version 1.0
 * @date 2021/11/4 16:36
 */
@Service
public class WxFseCraneServiceImpl implements WxFseCraneService {


    @DubboReference(interfaceClass = FseCraneService.class, version = "1.0.0")
    private FseCraneService fseCraneService;

    @DubboReference(interfaceClass = FseCraneDataService.class, version = "1.0.0")
    private FseCraneDataService fseCraneDataService;

    @DubboReference(interfaceClass = FvsDeviceService.class, version = "1.0.0")
    private FvsDeviceService fvsDeviceService;

    @Override
    public ListData<FseCraneDTO> list(Integer deptId) {
        return fseCraneService.list(deptId, BindFlag.BIND.getValue());
    }
    @Override
    public List<FseCraneDTO> listV2(Integer deptId) {
        List<FseCraneDTO> fseCraneDTOS = fseCraneService.listV2(deptId);
        fseCraneDTOS.forEach(fseCraneDTO -> {
            Integer craneId = fseCraneDTO.getCraneId();
            List<FseCraneFvsDeviceDTO> fseCraneFvsDeviceDTOS = fvsList(craneId);
            fseCraneDTO.setFvsList(fseCraneFvsDeviceDTOS);
            fseCraneDTO.setFvsDevices(null);
        });
        return fseCraneDTOS;
    }

    @Override
    public FseCraneDataDTO latest(Integer craneId) {
        return fseCraneDataService.latest(craneId);
    }

    @Override
    public ListData<FseCraneDataLogDTO> log(Integer craneId, Date date) {
        Date startTime = DateUtil.getDateBegin(date);
        Date endTime = DateUtil.getDateEnd(date);
        return fseCraneDataService.log(craneId, startTime, endTime);
    }


    @Override
    public List<FseCraneFvsDeviceDTO> fvsList(Integer craneId) {
        List<FseCraneFvsDeviceDTO> list = fseCraneService.getFvsDeviceList(craneId);
        if (list == null || list.isEmpty()) {
            return new ArrayList<>();
        }
        //取出设备ID 查找监控设备信息
        Map<Integer, FseCraneFvsDeviceDTO> map = new HashMap<>();
        for (FseCraneFvsDeviceDTO fseCraneFvsDeviceDTO : list) {
            map.put(fseCraneFvsDeviceDTO.getFvsDeviceId(), fseCraneFvsDeviceDTO);
        }
        List<FvsDeviceDTO> fvsDeviceList = fvsDeviceService.list(new ArrayList<>(map.keySet()));
        for (FvsDeviceDTO fvsDeviceDTO : fvsDeviceList) {
            FseCraneFvsDeviceDTO fseCraneFvsDeviceDTO = map.get(fvsDeviceDTO.getId());
            if (fseCraneFvsDeviceDTO != null) {
                fseCraneFvsDeviceDTO.setFvsDevice(fvsDeviceDTO);
            }
        }
        return new ArrayList<>(map.values());
    }
}
